package com.ita.iosa.jms.ptp;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

import javax.imageio.stream.ImageInputStream;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.servlet.ServletContext;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.catalina.Context;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;
import org.jdom.JDOMException;

import com.ita.iosa.dao.MerchantDao;
import com.ita.iosa.dao.MerchantStatusDao;
import com.ita.iosa.dao.impl.MerchantDaoImpl;
import com.ita.iosa.dao.impl.MerchantStatusDaoImpl;
import com.ita.iosa.po.Merchant;
import com.ita.iosa.po.MerchantStatus;
import com.ita.iosa.util.XmlResolverUtil;

public class Consumer {
	private MerchantDao mDao = new MerchantDaoImpl();
	private MerchantStatusDao mStatusDao = new MerchantStatusDaoImpl();
	private XmlResolverUtil xmlResolverUtil = new XmlResolverUtil();

	public void consume(String propertiesPath) throws Exception {
		Properties properties = new Properties();
		properties.load(new FileInputStream(propertiesPath));
	
		ConnectionFactory factory = new ActiveMQConnectionFactory(properties.getProperty("url"));
		Destination queue = new ActiveMQQueue(properties.getProperty("queue"));
		Connection con;
		
		try {
			con = factory.createConnection();
			con.start();
			Session session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
			MessageConsumer consumer = session.createConsumer(queue);

			consumer.setMessageListener(new MessageListener() {

				@Override
				public void onMessage(Message arg0) {
					TextMessage msg = (TextMessage) arg0;
					try {
						Merchant merchant = xmlResolverUtil.XMLToMerchant(msg);
					
						if (merchant.getMid() == -1) {
							int m = mDao.addMerchant(merchant);
							int mid = mDao.selectMerchantMid(merchant.getTel());
							int n = mStatusDao
									.addMerchantStatus(new MerchantStatus(mid, 0));

						}
						else {
							int m = mDao.updateMerchant(merchant);
							int mid=merchant.getMid();
							int findMid = mStatusDao.seleteMerchantStatusMid(mid);
							
							if (findMid == -1) {
								int n = mStatusDao.addMerchantStatus(new MerchantStatus(mid, 0));
							}
							else {
								int n = mStatusDao.updateMerchantStatus(mid, 0);
							}
						}
						
					} catch (IllegalArgumentException | SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			});
//			consumer.close();
//			session.close();
//			con.close();
		} catch (JMSException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		

		

	}
}
